﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;

namespace Texfina.Core.Web
{
   [ToolboxData("<{0}:esdFilterPanel Runat=server></{0}:esdFilterPanel>")]
   public class esdFilterPanel : WebControl
   {
      //private ScriptManager sm;

      [DefaultValue(FilterType.esdDateTexbox)]
      public FilterType FilterType
      { get; set; }

      [DefaultValue("")]
      public string Title
      { get; set; }

      [DefaultValue("")]
      public string ControlToken
      { get; set; }

      public DataTable DataSourceCombo
      { get; set; }

      [DefaultValue("")]
      public string DataSourceComboText
      { get; set; }

      [DefaultValue("")]
      public string DataSourceComboValue
      { get; set; }

      [DefaultValue("")]
      public string Identity
      { get; set; }

      [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
      protected override void RenderContents(HtmlTextWriter writer)
      {

         string idcontrolsearch = "";

         TextWriter RenderHtml = new StringWriter();
         RenderHtml.Write("<div id=\"" + this.ID + "Content" + "\" style=\" width:100%; height:100%;\">");
         RenderHtml.Write("   <a class=\"linkheader\" href=\"#\" id=\"" + this.ID + "href" + "\">" + this.Title + "</a>");
         RenderHtml.Write("      <div id=\"" + this.ID + "\" title=\"" + this.Title + "\">");
         RenderHtml.Write("         <select id=\"" + this.ID + "Operador" + "\" style=\" width:100%;\" >");
         if (FilterType != FilterType.esdDropDownList)
            RenderHtml.Write("            <option value=\"like\">Contiene</option>");

         RenderHtml.Write("            <option value=\"=\">Igual que</option>");
         RenderHtml.Write("            <option value=\"<\">Menor que</option>");
         RenderHtml.Write("            <option value=\"<=\">Menor Igual que</option>");
         RenderHtml.Write("            <option value=\">\">Mayor que</option>");
         RenderHtml.Write("            <option value=\">=\">Mayor Igual que</option>");
         RenderHtml.Write("         </select>");
         RenderHtml.Write("         <br/>");
         RenderHtml.Write("         <br/>");
         switch (FilterType)
         {
            case FilterType.esdDateTexbox:
               idcontrolsearch = this.ID + "Text";
               RenderHtml.Write("<input type=\"text\" id=\"" + idcontrolsearch + "\" name=\"" + idcontrolsearch + "\" class=\"esdTextBox\" style=\" width:294px;\" />");

               break;

            case FilterType.esdDropDownList:
               idcontrolsearch = this.ID + "Combo";
               RenderHtml.Write("         <select style=\"width:100%;\" id=\"" + idcontrolsearch + "\" >");
               if (this.DataSourceCombo != null && this.DataSourceCombo.Columns.Contains(this.DataSourceComboText) && this.DataSourceCombo.Columns.Contains(this.DataSourceComboText))
               {
                  foreach (DataRow dr in this.DataSourceCombo.Rows)
                  {
                     RenderHtml.Write("            <option value=\"" + dr[this.DataSourceComboValue].ToString() + "\">" + dr[this.DataSourceComboText].ToString() + "</option>");
                  }

               }
               RenderHtml.Write("         </select>");
               break;
            case FilterType.esdTextBox:
               break;
         }
         RenderHtml.Write("         <br/>");
         RenderHtml.Write("         <br/>");



         RenderHtml.Write("<input type=\"button\" value=\"Agregar Filtro\" id=\"" + this.ID + "Button" + "\" name=\"" + this.ID + "Button" + "\" class=\"esdButtonStyle\" style=\" width:296px;\" onclick=\"AddFilter('" + this.ID + "','" + this.ID + "Operador','" + idcontrolsearch + "','" + this.ControlToken + "','" + this.Title + "','" + this.Identity + "');\" />");



         RenderHtml.Write("      </div>");
         RenderHtml.Write("</div>");

         writer.Write(RenderHtml.ToString());

         StringBuilder sb = new StringBuilder();
         sb.AppendLine(" $(function () {");
         sb.AppendLine("    $(\"#" + this.ID + "\").dialog({");
         sb.AppendLine("        draggable: false,");
         sb.AppendLine("        autoOpen: false,");
         sb.AppendLine("        resizable: false,");
         sb.AppendLine("        show: { effect: \"blind\", duration: 300 },");
         sb.AppendLine("        modal: true");
         sb.AppendLine("    });");
         sb.AppendLine("    $(\"#" + this.ID + "href\").click(function () {");
         sb.AppendLine("        $(\"#" + this.ID + "\").dialog(\"open\");");
         if (FilterType == FilterType.esdDateTexbox)
         {
             sb.AppendLine("   document.getElementById('" + idcontrolsearch + "').value = '';");
         }

         sb.AppendLine("        return false;");
         sb.AppendLine("    });");
         sb.AppendLine("});");

         ScriptManager.RegisterStartupScript(this, this.GetType(), "SD" + this.ID, sb.ToString(), true);

      }

   }
}
